import com.jme3.asset.AssetManager;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

public class Level {
    // assetmanager, BulletAppState
    private AssetManager assetManager;
    private BulletAppState bulletAppState;

    
    //physics
    private RigidBodyControl    floor_phy;
    private RigidBodyControl	brick_phy;
    private RigidBodyControl	wall_phy;
    private RigidBodyControl	door_phy;
    
    // materials
    private Material wall_mat;
    private Material floor_mat;
    private Material door_mat;
	
    public Level(AssetManager as, BulletAppState bas) {
        this.assetManager = as;
        this.bulletAppState = bas;
        initMaterials();
    }
    
    @SuppressWarnings("deprecation")
	public Node makeSpaceStation() {
        Node stationNode = new Node("StationNode");
       
        // floor geometry
        Geometry floor = makeFloor();
        
        // wall geometry
        Geometry wall1 = makeWall(0.5f, 4f, 10f, 10f, 4f, 0f);
        Geometry wall2 = makeWall(0.5f, 4f, 10f, -10f, 4f, 0f);
        
        Geometry wall3 = makeWall(5f, 4f, 0.5f, 5f, 4f, 10f);
        Geometry wall4 = makeWall(10f, 4f, 0.5f, 0f, 4f, -10f);
        
        //Door geometry
        Geometry door1 = makeDoor(5f, 3.9f, 0.3f, -5f, 4f, 10f);
        
        //Create brick
        Vector3f testbrick = new Vector3f(0f, 2f, 15f);
        Geometry brick1 = makeBrick(testbrick);
        
        Vector3f testbrick2 = new Vector3f(0f, 7f, 15f);
        Geometry brick2 = makeBrick(testbrick2);

        // Add floor and player base nodes to rootNode
        stationNode.attachChild(brick1);
        stationNode.attachChild(brick2);
        stationNode.attachChild(floor);
        stationNode.attachChild(wall1);
        stationNode.attachChild(wall2);
        stationNode.attachChild(wall3);
        stationNode.attachChild(wall4);
        stationNode.attachChild(door1);
        return stationNode;   
    }
    
    private Geometry makeWall(float x, float y, float z, float xl, float yl, float zl){
        Box b = new Box(Vector3f.ZERO, x, y, z);
        if(x > y){
            b.scaleTextureCoordinates(new Vector2f(x, y));
        }else{
        	b.scaleTextureCoordinates(new Vector2f(z, y));
        }
        
        Geometry wall_geo = new Geometry("Wall", b);
        wall_geo.setMaterial(wall_mat);
        wall_geo.setLocalTranslation(xl, yl, zl);
        
        wall_phy = new RigidBodyControl(1.0f);
        wall_geo.addControl(wall_phy);
        wall_phy.setKinematic(true);

        bulletAppState.getPhysicsSpace().add(wall_phy);
        return wall_geo;
    }
    
    private Geometry makeDoor(float x, float y, float z, float xl, float yl, float zl){
        Box b = new Box(Vector3f.ZERO, x, y, z);
        if(x > y){
            b.scaleTextureCoordinates(new Vector2f(x, y));
        }else{
        	b.scaleTextureCoordinates(new Vector2f(z, y));
        }
        
        Geometry door_geo = new Geometry("Door", b);
        door_geo.setMaterial(door_mat);
        door_geo.setLocalTranslation(xl, yl, zl); 
        door_phy = new RigidBodyControl(1.0f);
        door_geo.addControl(door_phy);
        door_phy.setKinematic(true);
        bulletAppState.getPhysicsSpace().add(door_phy);
        return door_geo;
    }
    
    private Geometry makeFloor(){
        Box b = new Box(Vector3f.ZERO, 100f, 0.1f, 100f);
        b.scaleTextureCoordinates(new Vector2f(100, 100));
        Geometry floor_geo = new Geometry("Floor", b);
        floor_geo.setMaterial(floor_mat);
        floor_phy = new RigidBodyControl(0.0f);
        floor_geo.addControl(floor_phy);
        bulletAppState.getPhysicsSpace().add(floor_phy);
        return floor_geo;
    }
    
    @SuppressWarnings("deprecation")
	private Geometry makeBrick(Vector3f loc) {
        Box brick = new Box(Vector3f.ZERO, 2f, 2f, 2f);
        brick.scaleTextureCoordinates(new Vector2f(1f, 1f));
        Geometry brick_geo = new Geometry("brick", brick);
        brick_geo.setMaterial(wall_mat);
        brick_geo.setLocalTranslation(loc);
        brick_phy = new RigidBodyControl(1f);
        brick_geo.addControl(brick_phy);
        bulletAppState.getPhysicsSpace().add(brick_phy);
        return brick_geo;
      }
    
    private void initMaterials() {
    	wall_mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        TextureKey key = new TextureKey("Textures/walltile.png");
        key.setGenerateMips(true);
        Texture tex = assetManager.loadTexture(key);
        wall_mat.setTexture("DiffuseMap", tex);
        tex.setWrap(WrapMode.Repeat);
    	
        floor_mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        TextureKey key3 = new TextureKey("Textures/floortile.png");
        key3.setGenerateMips(true);
        Texture tex3 = assetManager.loadTexture(key3);
        tex3.setWrap(WrapMode.Repeat);
        floor_mat.setTexture("DiffuseMap", tex3);
        
        door_mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        TextureKey key2 = new TextureKey("Textures/doortile.png");
        key2.setGenerateMips(true);
        Texture tex2 = assetManager.loadTexture(key2);
        tex2.setWrap(WrapMode.Repeat);
        door_mat.setTexture("DiffuseMap", tex2);
      }

}
